package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.OrderInfoDao;
import com.clqteam.model.Product;
import com.clqteam.util.JDBCutil;

public class OrderInfoDaoImpl implements OrderInfoDao{	

	@Override
	public int getOrderProductAmountByOId(int oId) {
		int total = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT COUNT(s_product.pId) AS total");
		sql.append(" FROM s_order");
		sql.append(" INNER JOIN s_orderinfo");
		sql.append(" ON s_order.oId = s_orderinfo.oId");
		sql.append(" INNER JOIN s_product");
		sql.append(" ON s_product.pId = s_orderinfo.pId");
		sql.append(" INNER JOIN s_supplier");
		sql.append(" ON s_supplier.supId = s_product.supId");
		sql.append(" WHERE s_order.oId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				total = rs.getInt("total");
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return total;
	}
	

	@Override
	public List<Product> getOrderInfoByOId(int oId,int page,int rows) {
		List<Product> pros = new ArrayList<Product>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT s_product.pId,s_product.pName,s_product.pAmount,");
		sql.append(" s_product.pClass,s_product.pType,s_product.pCost,");
		sql.append(" s_product.pPrice,s_product.pTime,s_product.supId,");
		sql.append(" s_supplier.supName,s_orderinfo.poAmount");
		sql.append(" FROM s_order");
		sql.append(" INNER JOIN s_orderinfo");
		sql.append(" ON s_order.oId = s_orderinfo.oId");
		sql.append(" INNER JOIN s_product");
		sql.append(" ON s_product.pId = s_orderinfo.pId");
		sql.append(" INNER JOIN s_supplier");
		sql.append(" ON s_supplier.supId = s_product.supId");
		sql.append(" WHERE s_order.oId = ? LIMIT ?,?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oId);
			pst.setInt(2, (page-1)*rows);
			pst.setInt(3, rows);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Product pro = new Product();
				pro.setpAmount(rs.getInt("pAmount"));
				pro.setpClass(rs.getString("pClass"));
				pro.setpCost(rs.getDouble("pCost"));
				pro.setpId(rs.getInt("pId"));
				pro.setpName(rs.getString("pName"));
				pro.setPoAmount(rs.getInt("poAmount"));
				pro.setpPrice(rs.getDouble("pPrice"));
				pro.setpTime(rs.getString("pTime"));
				pro.setpType(rs.getString("pType"));
				pro.setSupId(rs.getInt("supId"));
				pro.setSupName(rs.getString("supName"));
				pros.add(pro);
			}
			JDBCutil.closeJDBC(pst, con);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return pros;
	}


	



}
